package Others;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import MainMethods.FileWr;
import MainMethods.HoverImage;
import MainMethods.Screenshot;

public class LogIn {
	String url;
	String login = "test_max_krertmm@jo-media.com";
	String pass = "123123";
	WebElement dropListMonth;
	WebElement dropListYear;
	int i = 1;
	HoverImage hover = new HoverImage();
	Screenshot scsh = new Screenshot();
	Date date = new Date();
	SimpleDateFormat dateFormatter = new SimpleDateFormat("MM.dd.yyyy");

	public void signUp(WebDriver driver, FileWr file, String screenDir)
			throws InterruptedException, NoSuchElementException {
		WebDriverWait wait = new WebDriverWait(driver, 10);
		System.out.println("Start sign up");
		file.writeFile("Start sign up");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		stop: if (hover
		// .doesElementExist(driver, By.id("username")) == true) {
				.doesElementExist(driver,
						By.xpath("//*[@id=\"signup\"]/div[2]")) == true) {
			userData(driver, i);
			// if e-mail already consists we change it
			if (hover.doesElementExist(driver, By.className("noscript_error")) == true) {
				// if (hover.doesElementExist(driver, By.className("reg-plan"))
				// == false) {
				i++;
				signUp(driver, file, screenDir);
				break stop;
			}
			System.out.println("loading page:" + driver.getCurrentUrl());
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			// fill users information
			driver.findElement(By.id("first-name")).sendKeys("qwerty");
			driver.findElement(By.id("last-name")).sendKeys("qwerty");
			driver.findElement(By.id("zip_code")).sendKeys("123");
			driver.findElement(By.id("ccn")).sendKeys("4000123440001230");
			driver.findElement(By.id("cvv2_code")).sendKeys("123");
			// select from drop list month
			dropListMonth = driver.findElement(By.id("month"));
			Select selectMonth = new Select(dropListMonth);
			selectMonth.selectByVisibleText("06 - jun");

			// select from drop list year
			dropListYear = driver.findElement(By.id("year"));
			Select selectYear = new Select(dropListYear);
			selectYear.selectByVisibleText("2014");
			// driver.findElement(By.id("checkbox")).click();
			driver.findElement(By.id("continue3")).click();
			try {
				Thread.sleep(2500);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			JavascriptExecutor jse = (JavascriptExecutor) driver;
			jse.executeScript("scroll(0, 450)");

			wait.until(ExpectedConditions.elementToBeClickable(By
					.xpath("/html/body/div[2]/div/span/form/input[2]")));

			driver.findElement(
					By.xpath("/html/body/div[2]/div/span/form/input[2]"))
					.click();
			// wait.until(ExpectedConditions.elementToBeClickable(By
			// .linkText("Continue as a Regular Member")));
			// driver.findElement(By.linkText("Continue as a Regular Member"))
			// .click();
			System.out.println("loading page:" + driver.getCurrentUrl());

			// wait.until(ExpectedConditions.elementToBeClickable(By
			// .id("unicef-button-2")));
			// driver.findElement(By.id("unicef-button-2")).click();
			//
			// wait.until(ExpectedConditions.elementToBeClickable(By
			// .id("continue-receipt")));
			// driver.findElement(By.id("continue-receipt")).click();

			url = "https://staging.membersarea.jomediainc.com/#front/home";
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			if (driver.getCurrentUrl().equals(url)) {
				System.out.println("signing up finished successful");
				file.writeFile("Signing up finished successful");
				file.writeFile("Login:" + dateFormatter.format(date) + i
						+ login);
				file.writeFile("Password:" + pass);
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {

					e.printStackTrace();
				}
				// log out from site
				logout(driver, file);
			} else {
				System.out.println("Signing up - FAILED");
				file.writeFile("Signing up - FAILED");
				reserveLogin(driver, file, screenDir);
			}
		}
	}

	// enter sign up user data
	private void userData(WebDriver driver, int i) {
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(
				dateFormatter.format(date) + i + login);
		driver.findElement(By.id("password")).sendKeys(pass);
		// driver.findElement(By.id("c-password")).sendKeys(pass);
		// driver.findElement(By.id("checkbox")).click();
		driver.findElement(By.className("button")).click();

	}

	// login users who are already signed up
	public void login(WebDriver driver, FileWr file)
			throws InterruptedException, NoSuchElementException {
		driver.navigate().to(
				"http://staging.membersarea.jomediainc.com/login#front/home");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(
				dateFormatter.format(date) + i + login);
		driver.findElement(By.id("password")).sendKeys(pass);
		driver.findElement(By.className("login_button")).click();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (hover
				.doesElementExist(driver, By.xpath("//*[@id=\"browse_page\"]")) == true) {
			System.out.println("login - works");
			file.writeFile("Log In - PASSED");
		} else {
			System.out.println("Log In  - FAILD");
			file.writeFile("Log In - FAILD");
		}
	}

	// login if sign up FAILED
	public void reserveLogin(WebDriver driver, FileWr file, String screenDir)
			throws InterruptedException, NoSuchElementException {
		driver.navigate().to("http://staging.membersarea.jomediainc.com/");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		scsh.makeScreen("login_page", screenDir);
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys("hrust@jo-media.com");
		driver.findElement(By.id("password")).sendKeys("123123");
		driver.findElement(By.className("login_button")).click();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		url = "http://staging.membersarea.jomediainc.com/#front/home";
		if (driver.getCurrentUrl().equals(url)) {
			System.out.println("login - works");
			file.writeFile("Log In - PASSED");
			scsh.makeScreen("home_page", screenDir);
			url = driver.getCurrentUrl();
			System.out.println(url);
		} else {
			System.out.println("Log In  - FAILD");
			file.writeFile("Log In - FAILD");
			try {
				file.closeWrite();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			driver.close();
		}
	}

	// logout from site
	public void logout(WebDriver driver, FileWr file) {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		WebElement logout;
		driver.findElement(By.xpath("//*[@id=\"account_container\"]/div[1]"))
				.click();
		logout = driver.findElement(By
				.xpath("//*[@id=\"account_container\"]/ul/li[7]/a"));
		logout.click();
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
		if (hover.doesElementExist(driver, By.className("container")) == true) {
			System.out.println("Log Out - PASSED");
			file.writeFile("Log Out - PASSED");
		} else {
			System.out.println("Log Out - FAILED");
			file.writeFile("Log Out - FAILED");
		}
	}

}
